package com.clothing.controller.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clothing.CommonAttributes;
import com.clothing.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clothing.bean.BusinessJson;
import com.clothing.controller.BaseController;
import com.clothing.entity.Business;
import com.clothing.entity.Industry;
import com.clothing.entity.User;
import com.clothing.entity.User.Status;
import com.clothing.service.BusinessService;
import com.clothing.service.IndustryService;
import com.clothing.service.UserService;
import com.clothing.utils.AjaxMsg;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Charles
 * @version 1.0
 * @ClassName: BusinessController
 * @Description: TODO(商家)
 * @date 2018年5月27日 上午11:14:04
 */
@RequestMapping("/app/business")
@Controller("appUserBusinessController")
public class BusinessController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private IndustryService industryService;
    @Autowired
    private BusinessService businessService;

    /**
     * 行业列表
     *
     * @return
     */
    @RequestMapping(value = "/getIndustries.json", method = RequestMethod.GET)
    @ResponseBody
    public String industries(Long parentId) {
        List<Industry> industries;
        if (null == parentId) {
            industries = industryService.findRoots();
        } else {
            Industry parent = industryService.find(parentId);
            if (parent != null) {
                industries = parent.getChildren();
            } else {
                return getParameterErrorMsg();
            }
        }
        if (Utils.isEmpty(industries)) {
            return getParameterErrorMsg();
        }
        JSONArray ja = new JSONArray();
        for (Industry industry : industries) {
            JSONObject jo = new JSONObject();
            jo.put("id", industry.getId());
            jo.put("name", industry.getName());
            jo.put("icon", industry.getFullIcon());
            ja.add(jo);
        }
        return new AjaxMsg(true, ja).toString();
    }

    /**
     * 申请入驻
     *
     * @param business
     * @param industryId
     * @return
     */
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    @ResponseBody
    public String apply(Business business, Long industryId) {
        if (!isValidNoMsg(business)) {
            return getParameterErrorMsg();
        }
        List<String> banners = business.getBanners();
        if (Utils.isEmpty(banners)) {
            return AjaxMsg.failed("请上传轮播图");
        }
        String prefix = CommonAttributes.SITE_URL;
        List<String> targetBanners = new ArrayList<>();
        Iterator<String> iterator = banners.iterator();
        while (iterator.hasNext()) {
            String image = iterator.next();
            if (Utils.isEmpty(image)) {
                iterator.remove();
            }
            if (image.startsWith(prefix)) {
                image = image.substring(prefix.length(), image.length());
            }
            targetBanners.add(image);
        }
        List<String> images = business.getImages();
        if (Utils.isEmpty(images)) {
            return AjaxMsg.failed("请上传详情图");
        }
        List<String> targetImages = new ArrayList<>();
        iterator = images.iterator();
        while (iterator.hasNext()) {
            String image = iterator.next();
            if (Utils.isEmpty(image)) {
                iterator.remove();
            }
            if (image.startsWith(prefix)) {
                image = image.substring(prefix.length(), image.length());
            }
            targetImages.add(image);
        }

        Industry industry = industryService.find(industryId);

        if (null == industry) {
            return AjaxMsg.failed("请选择行业");
        }

        User user = userService.getCurrent();

        if (Status.unApply != user.getStatus()) {
            return AjaxMsg.failed("你已经申请过了");
        }

        user.setStatus(Status.unChecked);
        business.setUser(user);
        business.setIndustry(industry);
        business.setBanners(targetBanners);
        business.setImages(targetImages);
        businessService.save(business);

        return AjaxMsg.success("申请成功");
    }

    /**
     * @param @param  id
     * @param @return 设定文件
     * @return String 返回类型
     * @throws
     * @Title: detail
     * @Description: TODO(商户详情)
     */
    @RequestMapping(value = "/getDetail.json", method = RequestMethod.GET)
    @ResponseBody
    public String detail(Long id) {
        Business business = businessService.find(id);

        if (null == business) {
            return getParameterErrorMsg();
        }

        BusinessJson json = new BusinessJson();
        json.setName(business.getName());

        return new AjaxMsg(true, json).toString();
    }
}
